package com.findtech.threePomelos.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetDataCallback;
import com.avos.avoscloud.ProgressCallback;
import com.findtech.threePomelos.R;
import com.findtech.threePomelos.base.MyActionBarActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.entity.BabyInfoEntity;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.utils.MyCalendar;
import com.findtech.threePomelos.utils.ToastUtil;
import com.findtech.threePomelos.utils.Tools;

import java.text.ParseException;
import java.util.List;

/**
 * Created by zhi.zhang on 5/3/16.
 */
public class BabyHealthTipsContentActivity extends MyActionBarActivity {

    private TextView bodyHealthContent;
    private ProgressDialog progressDialog;
    private BabyInfoEntity babyInfoEntity = BabyInfoEntity.getInstance();
    String tipType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_health_tip);
        MyApplication.getInstance().addActivity(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            tipType = bundle.getString(Tools.TIPS_TYPE, NetWorkRequest.FILE_HEALTH_TIPS);
            setToolbar(bundle.getString(Tools.TIPS_TYPE_TITLE,
                    getResources().getString(R.string.title_activity_baby_health_tip)), true);
        }
        bodyHealthContent = (TextView) findViewById(R.id.tv_body);
        requestBabyHealthDataAndUpdateUI(tipType);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    Handler mHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x77) {
                bodyHealthContent.setText(Html.fromHtml((String) msg.obj));
            }
            if (msg.what == 0x76) {
                Log.d("ZZ", "msg.obj = " + msg.obj);
            }
        }
    };

    private void showProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("加载中，请稍后...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();
    }

    private void dismissProgressDialog() {
        progressDialog.dismiss();
    }

    private void requestBabyHealthDataAndUpdateUI(final String type) {
        MyCalendar myCalendar;
        try {
            showProgressDialog();
            final String currentDate = Tools.getSystemTimeInChina("yyyy-MM-dd");
            if (TextUtils.isEmpty(babyInfoEntity.getBirthday())) {
                return;
            }
            String birthday = babyInfoEntity.getBirthday().replace("年", "-").replace("月", "-").replace("日", "");
            babyInfoEntity.setBabyTotalDay(this, birthday, "0");
            myCalendar = new MyCalendar(birthday, currentDate);
            Log.d("ZZ", "String is : " + myCalendar.getDateForHealthTips());
            AVQuery<AVObject> query = new AVQuery<>(NetWorkRequest.HEALTH_TIPS);
            query.whereEqualTo("Date", myCalendar.getDateForHealthTips());
            query.findInBackground(new FindCallback<AVObject>() {
                public void done(List<AVObject> avObjects, AVException e) {
                    if (e == null) {
                        if (avObjects.size() > 0) {
                            AVFile avFile = avObjects.get(0).getAVFile(type);
                            if (avFile == null) {
                                ToastUtil.showToast(BabyHealthTipsContentActivity.this, "数据异常！");
                                return;
                            }
                            avFile.getDataInBackground(new GetDataCallback() {
                                public void done(byte[] data, AVException e) {
                                    //process data or exception.
                                    dismissProgressDialog();
                                    if (e == null) {
                                        String body = new String(data);
                                        mHandle.obtainMessage(0x77, body).sendToTarget();
                                    } else {
                                        ToastUtil.showToast(BabyHealthTipsContentActivity.this, "网络异常，请稍后再试！");
                                    }
                                }
                            }, new ProgressCallback() {
                                @Override
                                public void done(Integer integer) {
                                    Log.d("ZZ", "integer.intValue = " + integer.intValue());
                                    mHandle.obtainMessage(0x76, integer).sendToTarget();
                                }
                            });
                        } else {
                            dismissProgressDialog();
                            ToastUtil.showToast(BabyHealthTipsContentActivity.this, "没有查到数据！");
                        }
                    } else {
                        dismissProgressDialog();
                        ToastUtil.showToast(BabyHealthTipsContentActivity.this, "网络异常，请稍后再试！");
                    }
                }
            });
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
